﻿Public Class 记录删除

    Dim RH_flag As Boolean

    '关闭窗体
    Private Sub 记录删除_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        RecordDelF = Nothing
    End Sub

    '出售记录
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        DataGridView1.Columns.Item("单号").Visible = True
        DataGridView1.Columns.Item("客户").Visible = True
        DataGridView1.Columns.Item("客户电话").Visible = True
        DataGridView1.Columns.Item("客户地址").Visible = True 


        RH_flag = False

        Dim sqlstr As String = "select * from 出售表"

        If RadioButton1.Checked Then
            Dim d1 As Date = Now
            Dim d2 As Date
            d1 = Date.Parse(d1.Year.ToString + "-" + d1.Month.ToString + "-" + "1")
            d2 = d1.AddMonths(1)
            sqlstr += " where 日期 >= #" + d1 + "# and 日期 < #" + d2 + "#"
        End If

        Dim ds As DataTable = SQLtoDataSet(sqlstr).Tables(0)
        DataGridView1.Rows.Clear()
        If ds.Rows.Count > 0 Then
            For i = 0 To ds.Rows.Count - 1
                Dim rs As DataRow = ds.Rows(i)
                Dim row1() As String = {rs.Item("ID").ToString, rs.Item("品牌").ToString, rs.Item("型号").ToString, "出售", rs.Item("售出单价").ToString, rs.Item("售出数量").ToString, rs.Item("出售总额").ToString, rs.Item("日期").ToString, rs.Item("备注1").ToString, rs.Item("单号").ToString, rs.Item("购机客户").ToString, rs.Item("联系电话").ToString, rs.Item("地址").ToString}
                DataGridView1.Rows.Add(row1)
            Next
        End If

    End Sub

    '入货记录
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        DataGridView1.Columns.Item("单号").Visible = False
        DataGridView1.Columns.Item("客户").Visible = False
        DataGridView1.Columns.Item("客户电话").Visible = False
        DataGridView1.Columns.Item("客户地址").Visible = False

        RH_flag = True

        Dim sqlstr As String = "select * from 入货表"

        If RadioButton1.Checked Then
            Dim d1 As Date = Now
            Dim d2 As Date
            d1 = Date.Parse(d1.Year.ToString + "-" + d1.Month.ToString + "-" + "1")
            d2 = d1.AddMonths(1)
            sqlstr += " where 日期 >= #" + d1 + "# and 日期 < #" + d2 + "#"
        End If

        Dim ds As DataTable = SQLtoDataSet(sqlstr).Tables(0)
        DataGridView1.Rows.Clear()
        If ds.Rows.Count > 0 Then
            For i = 0 To ds.Rows.Count - 1
                Dim rs As DataRow = ds.Rows(i)
                Dim row1() As String = {rs.Item("ID").ToString, rs.Item("品牌").ToString, rs.Item("型号").ToString, "入货", rs.Item("入货单价").ToString, rs.Item("数量").ToString, rs.Item("合计总额").ToString, rs.Item("日期").ToString, rs.Item("备注1").ToString}
                DataGridView1.Rows.Add(row1)
            Next
        End If
    End Sub

    '右键弹出菜单
    Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If DataGridView1.SelectedRows.Count > 0 Then
                ContextMenuStrip1.Visible = True
            Else
                ContextMenuStrip1.Visible = False
            End If
        End If

    End Sub

    '执行删除操作
    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
        If DataGridView1.SelectedRows.Count > 0 Then
            If MsgBox("你确定要删除选定的记录？", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                For i = 0 To DataGridView1.SelectedRows.Count - 1
                    Dim sqlstr As String = "delete from "
                    If RH_flag Then
                        sqlstr += "入货表"
                    Else
                        sqlstr += "出售表"
                    End If

                    sqlstr += " where ID =" + DataGridView1.SelectedRows(0).Cells("ID").Value
                    SQLupdata(sqlstr)
                    DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
                Next

            End If

        End If
    End Sub

End Class